import type { DictDataType, DictOptions, DictParams } from './types'
import { Observer } from './helper/observer'

export * from './types'
export * from './helper/utils'
export * from '@/enums/DICT_TYPE'

export const dictObserver = new Observer()

// 缓存优先的请求方法
export function requestDictData(params: DictParams) {
  return dictObserver.requestCache(params)
}

// useDictDatas
export function useDictDatas(params: DictParams, options: DictOptions = {}): { dictDatas: Ref<DictDataType[]> } {
  const dictDatas = ref<DictDataType[]>([]);

  dictObserver.getCache(params, (data) => {
    if (options.excludes && options.excludes.length) {
      data = data.filter((el: DictDataType) => !options.excludes?.includes(el.value))
    }

    if (options.isTree) {
    } else {
      dictDatas.value = data
    }
  })

  return {
    dictDatas
  }
}
